package dfbz.com.controller.conference;

import dfbz.com.controller.BaseServlet;
import dfbz.com.model.Conference;
import dfbz.com.service.ConferenceService;
import dfbz.com.service.DeptService;
import dfbz.com.service.UserService;
import dfbz.com.service.impl.ConferenceServiceImpl;
import dfbz.com.service.impl.DeptServiceImpl;
import dfbz.com.service.impl.UserServiceImpl;
import dfbz.com.util.StrUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@WebServlet("/conference/*")
public class ConferenceServlet extends BaseServlet {


    private ConferenceService conferenceService=new ConferenceServiceImpl();
    private DeptService deptService=new DeptServiceImpl();
    private UserService userService=new UserServiceImpl();


    public void show(HttpServletRequest request, HttpServletResponse response){
        try {
            String currentPage = request.getParameter("currentPage");
            String search = request.getParameter("search");
            String dep= request.getParameter("dep");
             //会议状态 0:未开始 1:进行中 2:已结束,更新会议状态
            conferenceService.updateStatus();

            int page = 1;
            if(null != currentPage){
                page = Integer.parseInt(currentPage);
            }
            if (null != search||null!=dep) {
                String encode = URLEncoder.encode(search, "UTF-8");
                    response.sendRedirect(request.getContextPath() + "/view/conList?page=" + page + "&search=" + encode+"&deptId=" + dep);

            }else {
                //刚登陆的用户来到这里

                response.sendRedirect(request.getContextPath() + "/view/conList?page="+page+"&deptId=0");
            }


        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public void add(HttpServletRequest request, HttpServletResponse response){

        try {

            String content = request.getParameter("content");
            String title = request.getParameter("title");

            String dep = request.getParameter("dep");
            String starttime = request.getParameter("starttime");
            System.out.println("starttime:"+starttime);
           //会议状态 0:未开始 1:进行中 2:已结束
            Integer status=0;
            Integer deptId=null;
            if(null!=dep){
                deptId= Integer.parseInt(dep);
            }
            String deptName =conferenceService.getNameById(deptId);
            Date pdate = new Date();

            //时间解码
            starttime = URLEncoder.encode(starttime, "UTF-8");

            //会议状态 0:未开始 1:进行中 2:已结束,更新会议状态
            conferenceService.updateStatus();
            Integer total = conferenceService.findLastConferenceCount();
            total++;//会议id
            List<Map<String, Object>> workers = deptService.showWorkers(deptId);
            Integer count=conferenceService.getConJoinCount();//获取conjoin的最大id
            count++;
            conferenceService.notice(workers,deptId,total,count);//deptId是部门编号，total是会议id，count是conjoinid
            conferenceService.add(deptName,deptId,title,content,pdate,starttime,status,total);
            response.sendRedirect("/view/conList?page=1&deptId=0");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showMeetDetail(HttpServletRequest request, HttpServletResponse response){
        String id1 = request.getParameter("id");
        Integer id=null;
            if (null!=id1){
               id = Integer.parseInt(id1.toString());//获取会议id

            }

            Map<String, Object> con = conferenceService.getMeetById(id);
            Integer did=null;
            if (null!=con){
                did =(Integer) con.get("did");//获得部门id
            }


            List<Map<String, Object>> workers = deptService.showWorkers(did);
            int size = workers.size();//部门总人数
            Long join=conferenceService.getJoinMeetDetail(id);//获取未参与会议人员的人数
            request.setAttribute("size",size);
            request.setAttribute("join",join);
            request.setAttribute("con",con);

        Map<String, Object> me = userService.getUserById((Integer) request.getSession().getAttribute("userId"));
        Integer deptId =(Integer) me.get("deptId");
        request.setAttribute("deptId",deptId);
        try {
            request.getRequestDispatcher("/view/showMeetDetail").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  void join(HttpServletRequest request, HttpServletResponse response){
        Object myId = request.getAttribute("myId");//用户id
        Object meetId = request.getAttribute("meetId");//会议id
        Integer id=null;
        Integer mid=null;
        if (null!=myId&&null!=meetId){
             id=Integer.parseInt(myId.toString());//用户id
            mid=Integer.parseInt(meetId.toString());//会议id
        }
        conferenceService.updateJoinStatus(id,mid);
        try {
            request.getRequestDispatcher("/conference/showMeetDetail?id="+mid).forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public  void updateStatus(HttpServletRequest request, HttpServletResponse response){
        Integer userId = (Integer)request.getSession().getAttribute("userId");
        String meetId = request.getParameter("meetId");
        conferenceService.updateJoinStatus(userId,Integer.parseInt(meetId));
        try {
            response.getWriter().write("success");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
